/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class MusikServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            /*String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            Class.forName(JDBC_DRIVER);
            String DATABASE_URL = "jdbc:mysql://localhost:3306/test_ikc019?user=root&password=admin";
            
            Connection conn = DriverManager.getConnection(DATABASE_URL);*/
            Connection conn = (Connection)getServletContext().getAttribute("Labb3");
            
            Statement statement;
            statement = conn.createStatement();
            ResultSet resultatMängd = statement.executeQuery("SELECT * FROM Music_Categories");
            
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MusikServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<form action='KategoriServlet'>");
            out.println("<select name='listbox'>");
            
            while(resultatMängd.next())
            {
                out.println("<option value=" + resultatMängd.getObject("name") + ">"+resultatMängd.getObject("name"));
            }
            out.println("</select>");
            
            out.println("<input type='submit'/>");
            out.println("</form>");
            out.println("<br/><br/>");
            out.println("<form action='Resultat3'>");
            out.println("<select name='val'>");
            out.println("<option value='album'>Album");
            out.println("<option value='titel'>Titel");
            out.println("<option value='artnr'>Artnr");
            out.println("<input type='text' name='text'/>");
            out.println("<input type='submit'/>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
            
            resultatMängd.close();
            statement.close();
           // conn.close();
        } 
       /* catch(ClassNotFoundException e)
        {
            out.println("Fel: " + e);
        }*/
        catch(SQLException e)
        {
            out.println("Fel: " + e);
        }
        finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
